﻿using System;
using System.Collections.Generic;
using System.Text;
using Pretext.Logging;

namespace Pretext {

	/// <summary>
	/// Represents a pretext session that can be shared across parsers
	/// </summary>
	public class PretextSession {

		readonly DateTime SessionStartTime = DateTime.Now;

		public event EventHandler<LogEventArgs> LogItem;

		public PretextSession() {

			Variables = new Dictionary<string, string>();

			Log = new List<LogItem>();

			SetCommonVariables();

		}

		public Dictionary<string, string> Variables { get; private set; }
		public List<LogItem> Log { get; private set; }
		public string DateCode { get; set; }
		
		public void SetCommonVariables() {

			this.Variables[":date_code"] = GetDateCode();

		}
	
		public void LogException(Exception ex, Token relatedToken) {

			AddLogItem(new LogItem(LogItemType.Error, ex.Message, relatedToken));

		}

		/// <summary>
		/// Adds an item to the log.
		/// </summary>
		/// <param name="item">The LogItem to add.</param>
		public void AddLogItem(LogItem item) {

			this.Log.Add(item);

			if (LogItem != null)
				LogItem(this, new LogEventArgs(item));

		}

		private string GetDateCode() {

			string _DateCode = "";
			string Time = SessionStartTime.ToShortTimeString();

			_DateCode += SessionStartTime.Year.ToString("D4");
			_DateCode += SessionStartTime.Month.ToString("D2");
			_DateCode += SessionStartTime.Day.ToString("D2");
			_DateCode += Time.Substring(0, 2) + Time.Substring(3);

			return _DateCode;

		}

	}

}